\mode<presentation>

\usepackage{etoolbox} % important
%\usepackage{xstring}
\usepackage{caption}
\captionsetup{compatibility=false}

% Maths font
\usefonttheme[onlymath]{serif}
%\DeclareMathSizes{10}{14}{10}{8}
%\DeclareMathSizes{display size}{text size}{script size}{scriptscript size}.

% Margins
\setbeamersize{text margin left=0.7cm,text margin right=0.7cm}

\SetupKeyvalOptions{%
    family=solarized,
    prefix=solarized@,
}
\DeclareBoolOption[false]{dark}
\DeclareBoolOption[false]{sectionframe} % Default: no frame at a begining of a section
\DeclareComplementaryOption{light}{dark}
\ProcessKeyvalOptions*

% Solarized palette
\definecolor{solarizedBase03}{HTML}{002B36}
\definecolor{solarizedBase02}{HTML}{073642}
\definecolor{solarizedBase01}{HTML}{586e75}
\definecolor{solarizedBase00}{HTML}{657b83}
\definecolor{solarizedBase0}{HTML}{839496}
\definecolor{solarizedBase1}{HTML}{93a1a1}
\definecolor{solarizedBase2}{HTML}{EEE8D5}
\definecolor{solarizedBase3}{HTML}{FDF6E3}
\definecolor{solarizedYellow}{HTML}{B58900}
\definecolor{solarizedOrange}{HTML}{CB4B16}
\definecolor{solarizedRed}{HTML}{DC322F}
\definecolor{solarizedMagenta}{HTML}{D33682}
\definecolor{solarizedViolet}{HTML}{6C71C4}
\definecolor{solarizedBlue}{HTML}{268BD2}
\definecolor{solarizedCyan}{HTML}{2AA198}
\definecolor{solarizedGreen}{HTML}{859900}

%% Set base colors for dark or light versions
%% Dark
\ifboolexpe{bool {solarized@dark}}{%
    \colorlet{solarizedRebase03}{solarizedBase03}
    \colorlet{solarizedRebase02}{solarizedBase02}
    \colorlet{solarizedRebase01}{solarizedBase01}
    \colorlet{solarizedRebase00}{solarizedBase00}
    \colorlet{solarizedRebase0}{solarizedBase0}
    \colorlet{solarizedRebase1}{solarizedBase1}
    \colorlet{solarizedRebase2}{solarizedBase2}
    \colorlet{solarizedRebase3}{solarizedBase3}
    \colorlet{solarizedAccent}{solarizedYellow}
    }{%
    %% Light
    \colorlet{solarizedRebase03}{solarizedBase3}
    \colorlet{solarizedRebase02}{solarizedBase2}
    \colorlet{solarizedRebase01}{solarizedBase1}
    \colorlet{solarizedRebase00}{solarizedBase0}
    \colorlet{solarizedRebase0}{solarizedBase00}
    \colorlet{solarizedRebase1}{solarizedBase01}
    \colorlet{solarizedRebase2}{solarizedBase02}
    \colorlet{solarizedRebase3}{solarizedBase03}
    \colorlet{solarizedAccent}{solarizedBase03}
}


% Text color
\setbeamercolor{normal text}{fg=solarizedRebase3, bg=solarizedRebase03}
\setbeamercolor{alerted text}{fg=solarizedAccent}
\setbeamercolor{example text}{fg=solarizedRebase1, bg=solarizedRebase02}

% Titles
\setbeamercolor*{titlelike}{fg=solarizedAccent}
\setbeamercolor*{frametitle}{fg=solarizedAccent}
\setbeamercolor*{framesubtitle}{fg=solarizedRed}
\setbeamercolor*{title}{fg=solarizedAccent}

% Font size
%\setbeamerfont{frametitle}{size=\scriptsize}
\setbeamerfont{framesubtitle}{size=\normalsize}

% References
%\hypersetup{hyperindex=false,bookmarksopen=false,colorlinks=true,urlcolor=solarizedRed,citecolor=solarizedCyan,allcolors=solarizedCyan,linkcolor=solarizedCyan}
\let\oldcite=\cite
\renewcommand{\cite}[1]{\textcolor{solarizedCyan}{\oldcite{#1}}}
%\renewcommand{\citep}[1]{(\textcolor{solarizedCyan}{\oldcite{#1}})}

% Template title page
\defbeamertemplate*{title page}{customized}[1][]{%
    \begin{center}
        \huge \inserttitle\\
        \vspace{0.2cm}
        \usebeamerfont{subtitle}\textcolor{solarizedRed}{\insertsubtitle}\\
        \vspace{1cm}
        \usebeamerfont{date}\insertdate\\
        \vspace{1cm}
        \usebeamerfont{author}\insertauthor\\
        \vspace{0.1cm}
        \usebeamerfont{institute}\insertinstitute
        \usebeamercolor[fg]{titlegraphic}\inserttitlegraphic%
    \end{center}
}

% Frame with section title
\ifboolexpe{bool {solarized@sectionframe}}{%
\setbeamertemplate{section page}{%
    \begin{center}
        \large \textcolor{solarizedRed}{Part~\insertsectionnumber.}
        \\
		\vspace{0.3cm}
		{\hypersetup{hidelinks}
			\Huge \insertsection%
		}
    \end{center}
}
\def\sectionpage{\usebeamertemplate*{section page}}
\AtBeginSection{\frame{\sectionpage}}
}{}

% Function converting numbers to letters for table of content numbering
\newcommand*{\myalpha}[1]{%
    \IfEqCase{#1}{%
        {1}{a}%
        {2}{b}%
        {3}{c}%
        {4}{d}%
        {5}{e}%
        {6}{f}%
        {7}{g}%
        {8}{h}%
        {9}{i}%
        {10}{j}%
        {11}{k}%
        {12}{l}%
    }
}

% Table of contents
\setbeamertemplate{section in toc}{{\hypersetup{hidelinks}\large \inserttocsectionnumber.~\textcolor{solarizedRed}{\inserttocsection}}}
\setbeamertemplate{subsection in toc}{\hspace{0.8em} \textcolor{solarizedRebase1}{\myalpha{\inserttocsubsectionnumber}.}~\inserttocsubsection\\}

% Header and footer from CSS 
%\setbeamertemplate{footline}[page number]{} % gets rid of bottom navigation bars
\setbeamertemplate{footline}[frame number]{}
\setbeamertemplate{navigation symbols}{} % gets rid of navigation symbols
\setbeamercolor{footline}{bg=solarizedRebase02,fg=solarizedRebase1}
\setbeamercolor{headline}{bg=solarizedRebase01,fg=solarizedRebase1}

% Figure Captions
\captionsetup[figure]{labelformat=empty} % No "Figure"
\captionsetup{font={color=solarizedCyan,scriptsize},labelfont={color=solarizedCyan,scriptsize}} % Cyan color, small font

\setbeamercolor*{structure}{bg=solarizedRebase01, fg=solarizedRebase1}

\setbeamercolor*{palette primary}{bg=solarizedRebase01, fg=solarizedRebase1}
\setbeamercolor*{palette secondary}{bg=solarizedRebase01, fg=solarizedRebase1}
\setbeamercolor*{palette tertiary}{bg=solarizedRebase01, fg=solarizedRebase1}
\setbeamercolor*{palette quaternary}{bg=solarizedRebase01, fg=solarizedRebase1}

% Make Blocks slightly lighter/darker
\setbeamercolor{block title}{fg=solarizedAccent, bg=solarizedRebase02}
\setbeamercolor{block body}{parent=normal text, bg=solarizedRebase02}

% same as footline
% Set Sidebar and footline colors
\setbeamercolor*{sidebar}{parent=headline}
\setbeamercolor*{palette sidebar primary}{fg=solarizedRebase01, fg=solarizedRebase1}
\setbeamercolor*{palette sidebar secondary}{fg=solarizedRebase01, fg=solarizedRebase1}
\setbeamercolor*{palette sidebar tertiary}{fg=solarizedRebase01, fg=solarizedRebase1}
\setbeamercolor*{palette sidebar quaternary}{fg=solarizedRebase01, fg=solarizedRebase1}

% border-color for headings
\setbeamercolor{separation line}{fg=solarizedRebase0}
\setbeamercolor{fine separation line}{fg=solarizedRebase0}

\setbeamercolor*{section in sidebar shaded}{parent=palette sidebar primary}
\setbeamercolor*{section in sidebar}{parent=palette sidebar primary, fg=solarizedRebase02}
\setbeamercolor*{subsection in sidebar}{parent=section in sidebar}
\setbeamercolor*{subsection in sidebar shaded}{parent=section in sidebar shaded}

% Bibliography
%\setbeamertemplate{bibliography item}[triangle]
\setbeamertemplate{bibliography item}{\hspace{+0.4cm}\scriptsize$\blacktriangleright$}
\setbeamercolor*{bibliography entry title}{fg=solarizedAccent}
\setbeamercolor*{bibliography entry author}{fg=solarizedMagenta}
%\setbeamercolor*{bibliography entry location}{fg=black}
%\setbeamercolor*{bibliography entry note}{fg=black}

\mode
<all>
